package com.byapp.fast.common.converter;

import com.byapp.fast.modules.match.dto.MatchBetDTO;
import com.byapp.fast.modules.match.dto.MatchOddsDTO;
import com.byapp.fast.modules.match.entity.BaseOddsEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

public class MatchBetDTOConverter {

    private MatchBetDTOConverter() {
    }

    private static MatchBetDTO create(List<MatchOddsDTO> list, String playId) {
        MatchBetDTO matchBetDTO = new MatchBetDTO();
        List<MatchOddsDTO> oddsList = list.stream()
                .filter(matchOddsDTO -> StringUtils.equals(matchOddsDTO.getPlayId(), playId))
                .collect(Collectors.toList());
        matchBetDTO.setPlayId(playId);
        matchBetDTO.setOddsList(oddsList);
        return matchBetDTO;
    }


    public static List<MatchBetDTO> mapToTarget(List<BaseOddsEntity> list) {
        List<MatchBetDTO> result = null;
        List<MatchOddsDTO> oddsDTOList = MatchOddsDTOConverter.mapToTarget(list);
        if (!CollectionUtils.isEmpty(oddsDTOList)) {
            List<String> playIds = oddsDTOList.stream().map(MatchOddsDTO::getPlayId).distinct().collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(playIds)) {
                result = playIds.stream().map(playId -> MatchBetDTOConverter.create(oddsDTOList, playId)).collect(Collectors.toList());
            }
        }
        return result;
    }

}
